package com.bsoft.gol.editor.service.impl;


import com.bsoft.gol.editor.dao.EditorCommonWidgetDAO;
import com.bsoft.gol.editor.dao.EditorQuestionnaireTitleDAO;
import com.bsoft.gol.editor.dao.EditorTitleOptionDAO;
import com.bsoft.gol.editor.editor.EditorCommonWidget;
import com.bsoft.gol.editor.editor.EditorQuestionnaireTitle;
import com.bsoft.gol.editor.editor.EditorTitleOption;
import com.bsoft.gol.editor.editor.response.EditorCommonWidgetRes;
import com.bsoft.gol.editor.editor.response.EditorQuestionnaireTitleRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bsoft.gol.editor.service.EditorCommonWidgetApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.*;


/**
 * 常用控件题目表
 * @author sumu
 */
@Service("editorCommonWidgetApi")
public class EditorCommonWidgetApiImpl implements EditorCommonWidgetApi {
    private static Logger logger = LoggerFactory.getLogger(EditorCommonWidgetApiImpl.class);

    @Autowired
    private EditorCommonWidgetDAO editorCommonWidgetDAO;
    @Autowired
    private EditorQuestionnaireTitleDAO editorQuestionnaireTitleDAO;
    @Autowired
    private EditorTitleOptionDAO editorTitleOptionDAO;


    @Override
    @Transactional(rollbackFor = Exception.class)
    @Deprecated
    public EditorCommonWidgetRes add(EditorCommonWidget editorCommonWidget, String titleName) {

        EditorQuestionnaireTitle title = new EditorQuestionnaireTitle();
        String titleId = UUID.randomUUID().toString();
        title.setTitleId(titleId);
        title.setCreateTime(new Date());
        title.setWidgetType(editorCommonWidget.getCommonWidgetType());
        title.setTitleName(titleName);

        editorQuestionnaireTitleDAO.save(title);


        EditorCommonWidget commonWidget = new EditorCommonWidget();
        String widgetId = UUID.randomUUID().toString();
        BeanUtils.copyProperties(editorCommonWidget, commonWidget);
        commonWidget.setCommonWidgetId(widgetId);
        commonWidget.setTitleId(titleId);
        editorCommonWidgetDAO.save(commonWidget);

        EditorCommonWidgetRes res = new EditorCommonWidgetRes();
        res.setTitleId(titleId);
        res.setWidgetId(widgetId);

        return res;
    }

    @Override
    public EditorQuestionnaireTitleRes detail(String widgetId) {
        EditorQuestionnaireTitleRes titleRes = new EditorQuestionnaireTitleRes();
        EditorCommonWidget commonWidget = editorCommonWidgetDAO.get(widgetId);

        if (CommonUtils.isNotEmpty(commonWidget)) {
            titleRes.setWidgetId(commonWidget.getCommonWidgetId());
            EditorQuestionnaireTitle title = editorQuestionnaireTitleDAO.get(commonWidget.getTitleId());
            EditorTitleOption editorTitleOption = new EditorTitleOption();
            editorTitleOption.setTitleId(title.getTitleId());
            List<EditorTitleOption> options = editorTitleOptionDAO.queryEditorTitleOptionByTitleId(title.getTitleId());
            if (CommonUtils.isNotEmpty(options)) {
                List<EditorTitleOption> resList = new ArrayList<>();

                for (EditorTitleOption option : options) {
                    EditorTitleOption optionRes = new EditorTitleOption();
                    BeanUtils.copyProperties(option, optionRes);
                    resList.add(optionRes);
                }
                titleRes.setOptions(resList);
            }

        }

        return titleRes;
    }

    @Override
    public void delete(String widgetId) {
        editorCommonWidgetDAO.remove(widgetId);
    }

    @Override
    public List<EditorCommonWidgetRes> list(String classId) {

        List<EditorCommonWidget> widgets = editorCommonWidgetDAO.queryEditorCommonWidgetByClassId(classId);
        List<EditorCommonWidgetRes> res = new ArrayList<>();
        if (CommonUtils.isNotEmpty(widgets)) {
            List<String> titleIds = new ArrayList<>();
            for (EditorCommonWidget widget : widgets) {
                titleIds.add(widget.getTitleId());
                EditorQuestionnaireTitle title = editorQuestionnaireTitleDAO.get(widget.getTitleId());

                EditorCommonWidgetRes res1 = new EditorCommonWidgetRes();
                BeanUtils.copyProperties(title, res1);

                res1.setWidgetId(widget.getCommonWidgetId());
                res1.setWidgetDesc(widget.getCommonWidgetDesc());
                res1.setWidgetName(widget.getCommonWidgetName());
                res1.setWidgetType(widget.getCommonWidgetType());
                res.add(res1);
            }

            List<EditorTitleOption> options = editorTitleOptionDAO.queryEditorTitleOptionByTitleIds(titleIds);

            if (CommonUtils.isNotEmpty(options)) {

                Map<String, List<EditorTitleOption>> map = new HashMap<>(options.size());
                for (EditorTitleOption option : options) {

                    EditorTitleOption optionRes = new EditorTitleOption();
                    BeanUtils.copyProperties(option, optionRes);

                    if (map.containsKey(option.getTitleId())) {
                        List<EditorTitleOption> resList = map.get(option.getTitleId());
                        resList.add(optionRes);

                    } else {
                        List<EditorTitleOption> resList = new ArrayList<>();

                        resList.add(optionRes);
                        map.put(optionRes.getTitleId(), resList);
                    }
                }

                for (EditorCommonWidgetRes titleRes : res) {
                    List<EditorTitleOption> resList = map.get(titleRes.getTitleId());
                    try {
                        resList.sort(new Comparator<EditorTitleOption>() {
                            @Override
                            public int compare(EditorTitleOption o1, EditorTitleOption o2) {
                                return o1.getOptionNum().compareTo(o2.getOptionNum());
                            }
                        });
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                    titleRes.setOptions(resList);
                }

            }


        }
        return res;
    }
}